//
//  EntrepreneurViewController.m
//  BigToBig
//
//  Created by liuyi on 7/18/16.
//  Copyright © 2016 cn.cloudtp. All rights reserved.
//

#import "EntrepreneurViewController.h"
#import "HomeFunctionView.h"
#import "HomeEntrepreneurTableViewCell.h"
#import "EntrepreneurDetailMessageController.h"

@interface EntrepreneurViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource
>
@property (nonatomic , retain)HomeFunctionView *homeFunctionView;
@property (nonatomic , retain)UITableView *mainTableView;
@property (nonatomic , assign)NSInteger offset;
@property (nonatomic , strong)NSMutableArray *entrepreneurModelArr;
@property (nonatomic , assign)NSInteger modelCount;


@end

@implementation EntrepreneurViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    // 创建功能区(排序 and 筛选)
    self.homeFunctionView = [[HomeFunctionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    [self.view addSubview:self.homeFunctionView];
    
    
    // 创建mainTableView
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, kScreenWidth, kScreenHeight - 64 - 84 - 49)];
    [self.view addSubview:self.mainTableView];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.mainTableView registerClass:[HomeEntrepreneurTableViewCell class] forCellReuseIdentifier:NSStringFromClass([HomeEntrepreneurTableViewCell class])];
    self.mainTableView.separatorStyle = NO;
    self.mainTableView.bounces = NO;
    [self createData];
}

- (void)createData{
    self.entrepreneurModelArr = [NSMutableArray array];
    NSString *urlStr = [NSString stringWithFormat:@"http://61.135.176.43:6080/bigtobig/basic/user/get_users?user_token=1&user_type=1&county=&province=&city=&industry=&limit=10&offset=%ld",self.offset];
    NSDictionary *params = @{@"offset":[NSString stringWithFormat:@"%ld",self.offset]};
    [HttpTool textHTMLPostWithURL:urlStr params:params success:^(id json) {
        NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSArray *userListArr = [dict objectForKey:@"userlist"];
        for (NSDictionary *userDic in userListArr) {
            UserMessageModel *model = [UserMessageModel UserMessageModelWithDict:userDic];
            [self.entrepreneurModelArr addObject:model];

        }
        NSString *countStr = [dict objectForKey:@"count"];
        self.modelCount = self.modelCount + countStr.integerValue;
        [self.mainTableView reloadData];
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];

}

#pragma mark - tableView 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeEntrepreneurTableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeEntrepreneurTableViewCell class]) forIndexPath:indexPath];
    UserMessageModel *model = self.entrepreneurModelArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://61.135.176.43:6080/data/HeadPortrait/%@",model.headImage]] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    cell.titleLabel.text = model.name;
    if (model.selfIntroduction != nil) {
        cell.describeLabel.text = model.selfIntroduction;
    }else{
        cell.describeLabel.text = @"这个人很懒 什么信息都没有留下.";
    }
    cell.focusLabel.text = [NSString stringWithFormat:@"  已关注 : %@",model.myNoticeCount];
    cell.funsLabel.text = [NSString stringWithFormat:@"  粉丝 : %@",model.noticeToMeCount];
    return cell;
}

// cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EntrepreneurDetailMessageController *entrepreneurDetailVC  = [[EntrepreneurDetailMessageController alloc]init];
    self.tabBarController.tabBar.hidden = YES;
    UserMessageModel *model = self.entrepreneurModelArr[indexPath.row];
    entrepreneurDetailVC.model = model;
    [self.navigationController pushViewController:entrepreneurDetailVC animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
